package com.zsj.springbootmq.controller;

import com.zsj.springbootmq.pojo.EmailInfo;
import com.zsj.springbootmq.pojo.LogInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/**
 * @author https://gitee.com/zhengshengjun
 * @date 2023/11/17.
 */
@RestController
@Slf4j
@RequestMapping("/mq")
public class RabbitMqController {

    @Autowired
    RabbitTemplate rabbitTemplate;


    @GetMapping("/send")
    public String sendMsg() {
        for (int i = 0; i < 10; i++) {
            if (i % 2 == 0) {
                LogInfo logInfo = new LogInfo();
                logInfo.setUuid(UUID.randomUUID().toString());
                logInfo.setRequestURL("api/system/user/info");
                logInfo.setCreate(new Date(System.currentTimeMillis()));
                rabbitTemplate.
                        convertAndSend("java-direct-exchange"
                                , "java-q1",
                                logInfo, new CorrelationData(UUID.randomUUID().toString()));
                //application/x-java-serialized-object
                log.info("消息发送完成{}", logInfo);
            } else {
                EmailInfo emailInfo = new EmailInfo();
                emailInfo.setTo("zsj");
                emailInfo.setFrom("baidu");
                emailInfo.setUuid(UUID.randomUUID().toString());
                rabbitTemplate.
                        convertAndSend("java-direct-exchange"
                                , "java-q1",
                                emailInfo, new CorrelationData(UUID.randomUUID().toString()));
                //application/x-java-serialized-object
                log.info("消息发送完成{}", emailInfo);
            }
        }

        return "send message success";
    }
}
